package com.peza.bikelog.common.domain.polar;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.peza.bikelog.common.util.Util;

public class HrmInterval implements Writable{

	private static Util util = Util.getInstance();
	
	// Row 1
	private String lapTime;
	private int momentaryHr;
	private int minHr;
	private int avgHr;
	private int maxHr;
	
	// Row 2
	private String flags; //8-bit notation
	private long recTime; //seconds
	private int recHr; //bpm
	private int momentarySpeed; //km/h
	private int momentaryCadence; //rpm
	private int momentatyAltitude; //m
	
	// Row 3
	private int extra1;
	private int extra2;
	private int extra3;
	private int lapAscent; //10m
	private int lapDistance; //100m
	
	// Row 4
	private String lapType;
	private int manualLapDistance; //meters
	private int momentaryPower; //watts
	private int momentaryTemp; // celsius x10
	private String phaseLap;
	private String airPressure;
	
	// Row 5
	private int strideAvg;
	private boolean autoLap;
	private String extra4;
	private String extra5;
	private String extra6;
	private String extra7;

	
	
	
	public HrmInterval(){
		
	}
	
	public HrmInterval(String data){
		
		List<String> rows = Arrays.asList(data.split("\n"));
		Iterator<String> i = rows.iterator();
		
		while (i.hasNext()){
			
			// Row 1
			String row = i.next();
			String[] fields = row.split("\t");
			this.lapTime = fields[0];
			this.momentaryHr = util.convertToInt(fields[1]);
			this.minHr = util.convertToInt(fields[2]);
			this.avgHr = util.convertToInt(fields[3]);
			this.maxHr = util.convertToInt(fields[4]);
			
			// Row 2
			row = i.next();
			fields = row.split("\t");
			flags = fields[0];
			recTime = util.convertToLong(fields[1]); //seconds
			recHr = util.convertToInt(fields[2]);
			momentarySpeed = util.convertToInt(fields[3]);
			momentaryCadence = util.convertToInt(fields[4]);
			momentatyAltitude = util.convertToInt(fields[5]);
			
			// Row 3
			row = i.next();
			fields = row.split("\t");
			extra1 = util.convertToInt(fields[0]);
			extra2 = util.convertToInt(fields[1]);
			extra3 = util.convertToInt(fields[2]);
			lapAscent = util.convertToInt(fields[3]);
			lapDistance = util.convertToInt(fields[4]);
			
			// Row 4
			row = i.next();
			fields = row.split("\t");
			lapType = fields[0];
			manualLapDistance = util.convertToInt(fields[1]);
			momentaryPower = util.convertToInt(fields[2]);
			momentaryTemp = util.convertToInt(fields[3]);
			phaseLap = fields[4];
			airPressure = fields[5];
			
			// Row 5
			row = i.next();
			fields = row.split("\t");
			strideAvg = util.convertToInt(fields[0]);
			autoLap = util.convertToBoolean(fields[1]);
			extra4 = fields[2];
			extra5 = fields[3];
			extra6 = fields[4];
			extra7 = fields[5];

		}
	
		
	}

	
	
	@Override
	public String toFileString() {
		StringBuffer buffer = new StringBuffer();
		String autoLapValue = (autoLap ? "1" : "0");
		buffer.append(lapTime + "\t" + momentaryHr + "\t" +  minHr + "\t" +  avgHr + "\t" +  maxHr + "\n");
		buffer.append(flags + "\t" + recTime + "\t" +  recHr + "\t" +  momentarySpeed + "\t" +  momentaryCadence + "\t" +  momentatyAltitude + "\n");
		buffer.append(extra1 + "\t" +  extra2 + "\t" +  extra3 + "\t" +  lapAscent + "\t" +  lapDistance + "\n");
		buffer.append(lapType + "\t" + manualLapDistance + "\t" + momentaryPower + "\t" + momentaryTemp + "\t" + phaseLap + "\t" + airPressure + "\n");
		buffer.append(strideAvg + "\t" + autoLapValue +"\t"+ extra4 +"\t"+ extra5 +"\t"+ extra6 +"\t"+ extra7+ "\n");
		buffer.append("\n");
		return buffer.toString();
	}

	public String getLapTime() {
		return lapTime;
	}

	public void setLapTime(String lapTime) {
		this.lapTime = lapTime;
	}

	public int getMomentaryHr() {
		return momentaryHr;
	}

	public void setMomentaryHr(int momentaryHr) {
		this.momentaryHr = momentaryHr;
	}

	public int getMinHr() {
		return minHr;
	}

	public void setMinHr(int minHr) {
		this.minHr = minHr;
	}

	public int getAvgHr() {
		return avgHr;
	}

	public void setAvgHr(int avgHr) {
		this.avgHr = avgHr;
	}

	public int getMaxHr() {
		return maxHr;
	}

	public void setMaxHr(int maxHr) {
		this.maxHr = maxHr;
	}

	public String getFlags() {
		return flags;
	}

	public void setFlags(String flags) {
		this.flags = flags;
	}

	public long getRecTime() {
		return recTime;
	}

	public void setRecTime(long recTime) {
		this.recTime = recTime;
	}

	public int getRecHr() {
		return recHr;
	}

	public void setRecHr(int recHr) {
		this.recHr = recHr;
	}

	public int getMomentarySpeed() {
		return momentarySpeed;
	}

	public void setMomentarySpeed(int momentarySpeed) {
		this.momentarySpeed = momentarySpeed;
	}

	public int getMomentaryCadence() {
		return momentaryCadence;
	}

	public void setMomentaryCadence(int momentaryCadence) {
		this.momentaryCadence = momentaryCadence;
	}

	public int getMomentatyAltitude() {
		return momentatyAltitude;
	}

	public void setMomentatyAltitude(int momentatyAltitude) {
		this.momentatyAltitude = momentatyAltitude;
	}

	public int getExtra1() {
		return extra1;
	}

	public void setExtra1(int extra1) {
		this.extra1 = extra1;
	}

	public int getExtra2() {
		return extra2;
	}

	public void setExtra2(int extra2) {
		this.extra2 = extra2;
	}

	public int getExtra3() {
		return extra3;
	}

	public void setExtra3(int extra3) {
		this.extra3 = extra3;
	}

	public int getLapAscent() {
		return lapAscent;
	}

	public void setLapAscent(int lapAscent) {
		this.lapAscent = lapAscent;
	}

	public int getLapDistance() {
		return lapDistance;
	}

	public void setLapDistance(int lapDistance) {
		this.lapDistance = lapDistance;
	}

	public String getLapType() {
		return lapType;
	}

	public void setLapType(String lapType) {
		this.lapType = lapType;
	}

	public int getManualLapDistance() {
		return manualLapDistance;
	}

	public void setManualLapDistance(int manualLapDistance) {
		this.manualLapDistance = manualLapDistance;
	}

	public int getMomentaryPower() {
		return momentaryPower;
	}

	public void setMomentaryPower(int momentaryPower) {
		this.momentaryPower = momentaryPower;
	}

	public int getMomentaryTemp() {
		return momentaryTemp;
	}

	public void setMomentaryTemp(int momentaryTemp) {
		this.momentaryTemp = momentaryTemp;
	}

	public String getPhaseLap() {
		return phaseLap;
	}

	public void setPhaseLap(String phaseLap) {
		this.phaseLap = phaseLap;
	}

	public String getAirPressure() {
		return airPressure;
	}

	public void setAirPressure(String airPressure) {
		this.airPressure = airPressure;
	}

	public int getStrideAvg() {
		return strideAvg;
	}

	public void setStrideAvg(int strideAvg) {
		this.strideAvg = strideAvg;
	}

	public boolean isAutoLap() {
		return autoLap;
	}

	public void setAutoLap(boolean autoLap) {
		this.autoLap = autoLap;
	}
	
	
}
